//
//  BuildingViewController.m
//  Kfangchan
//
//  Created by liran on 14/11/5.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "BuildingViewController.h"
#import "BuildingTableViewCell.h"
#import "BuildingDetailViewController.h"
#import "NewCustomerViewController.h"
#import "MyInfoViewController.h"
#import "LoginViewController.h"

@interface BuildingViewController ()

@property (nonatomic) BOOL   needLogin;
@property (nonatomic) int cpage;
@property (strong, nonatomic) NSMutableArray *buildingArray;
@property (strong, nonatomic) IBOutlet UITableView *buildingTableView;

@end

@implementation BuildingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.needLogin = NO;
    
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"热销楼盘"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:[UIImage imageNamed:@"building_top_left.png"] Target:nil Selector:nil];
    self.navigationItem.rightBarButtonItem = [[Theam currentTheam] navigationBarRightButtonItemWithImage:[UIImage imageNamed:@"building_top_right.png"] Target:self Selector:@selector(personal)];
    
    self.buildingTableView.size = CGSizeMake(self.buildingTableView.width, ScreenHeight - 65);
    

    __weak BuildingViewController *weak_self = self;
    //header
    [self.buildingTableView addHeaderWithCallback:^{
        [weak_self initList];
    } andIsChat:YES andIsOther:YES];
    //footer
    [self.buildingTableView addFooterWithCallback:^{
        weak_self.cpage++;
        [weak_self loadData];
    }];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSDictionary *agent = [AppConfig getAgent];
    if (agent != nil && agent.count > 0) {
        if (theApp.reloadBuildingList) {
            self.buildingArray = nil;
            theApp.reloadBuildingList = NO;
        }
        if (self.buildingArray == nil || self.buildingArray.count == 0) {
            [self initList];
        }
    } else {
        [self goLogin:NO];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark 右上角跳我的资料
- (void)personal {
//    IMP_BLOCK_SELF(BuildingViewController);
    MyInfoViewController *myinfo = [[MyInfoViewController alloc] init];
    myinfo.hidesBottomBarWhenPushed = YES;
//    myinfo.infoHandler = ^(id sender){
//        block_self.needLogin = YES;
//    };
    [self.navigationController pushViewController:myinfo animated:YES];}

#pragma mark - 列表相关
#pragma mark 初始化
- (void)initList {
    self.cpage = 1;
    if (self.buildingArray == nil) {
        self.buildingArray = [NSMutableArray array];
    }
    [self loadData];
}

#pragma mark 加载数据
- (void)loadData {
    __weak BuildingViewController *weak_self = self;
    NSDictionary *param = @{@"cpage": [NSString stringWithFormat:@"%i", self.cpage]};
    MKNetworkOperation *operation = [[BaseEngine sharedEngine]RunRequest:[param mutableCopy] path:KFC_BUILDING_LIST completionHandler:^(id responseObject) {
        if (weak_self.cpage == 1) {
            [weak_self.buildingArray removeAllObjects];
        }
        
        id totalPages = [responseObject objectForKey:@"totalPages"];
        if (weak_self.cpage >= [totalPages intValue]) {
            [weak_self.buildingTableView setFooterHidden:YES];
        } else {
            [weak_self.buildingTableView setFooterHidden:NO];
        }
        
        id list = [responseObject objectForKey:@"list"];
        if (![list isKindOfClass:[NSNull class]]) {
            BuildingList *buildingList = [[BuildingList alloc] initWithArray:list];
            [weak_self.buildingArray addObjectsFromArray:buildingList.array];
        }
        
        [weak_self.buildingTableView reloadData];
    } errorHandler:^(NSError *error) {
        showCustomAlertMessage(@"网路错误");
        [weak_self.buildingTableView headerEndRefreshing];
        [weak_self.buildingTableView setFooterHidden:YES];
    } finishHandler:^(id responseObject) {
        [weak_self.buildingTableView headerEndRefreshing];
        [weak_self.buildingTableView footerEndRefreshing];
    }];
    [self.opHandlers addObject:operation];
}

#pragma mark - UITableView
#pragma mark 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.buildingArray count];
}

#pragma mark 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"BuildingTableViewCell";
    
    Building *entity = self.buildingArray[indexPath.row];
    
    BuildingTableViewCell *cell = (BuildingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        [tableView registerNib:[UINib nibWithNibName:identifier bundle:nil] forCellReuseIdentifier:identifier];
        cell = (BuildingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    }
    
    __weak BuildingViewController *weak_self = self;
    cell.handle = ^(id param) {
        [weak_self recommendCustomer:[param intValue]];
    };
    [cell initCell:entity];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BuildingDetailViewController *bvc = [[BuildingDetailViewController alloc] init];
    bvc.entity = self.buildingArray[indexPath.row];
    bvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:bvc animated:YES];
}

#pragma mark -
#pragma mark 推荐客户
- (void)recommendCustomer:(int)pkid {
    NewCustomerViewController *nvc = [[NewCustomerViewController alloc] init];
    nvc.hidesBottomBarWhenPushed = YES;
    nvc.buildingId = pkid;
    [self.navigationController pushViewController:nvc animated:YES];
}

@end
